const http = require('../../../../utils/http.js')
const util = require('../../../../utils/util.js')

Page({

    /**
     * 页面的初始数据
     */
    data: {
        imgUrl: http.imgUrl,
        chosen: [],
        chosenArr: [],
        ltArr: [],
        handOver: false,
        noComplete: true,
        has_miss: true,
        nullData: {
            img: http.imgUrl + 'null_data_1.jpg',
            txt: '此测试题中暂时没有题目'
        }
    },

    /**
     * 返回
     */
    goBack() {
        http.goBack()
    },

    /**
     * 选择单选或者判断答案
     */
    chooseAnswer(e) {
        let that = this,
            index = http.dataIndex(e)[0],
            temp_chosen = that.data.chosen,
            nowNum = that.data.nowNum

        temp_chosen[nowNum - 1] = index + 1
        that.setData({
            chosen: temp_chosen
        })

        let temp_ltArr = that.data.ltArr,
            ttid = that.data.ttid,
            temp_ttid = [],
            chosen = that.data.chosen

        for (let item of temp_ltArr) {
            temp_ttid.push(item.ttid)
        }

        if (temp_ttid.indexOf(ttid) === -1) {
            temp_ltArr.push(
                {
                    ttid: ttid,
                    my_answer: (chosen[index] - 1).toString()
                }
            )
        } else {
            temp_ltArr[temp_ttid.indexOf(ttid)] = {
                ttid: ttid,
                my_answer: (chosen[index] - 1).toString()
            }
        }

        that.setData({
            ltArr: temp_ltArr
        })
    },

    /**
     * 选择多选答案
     */
    chooseAnswerMore(e) {
        let that = this,
            index = http.dataIndex(e)[0],
            chosenArr = that.data.chosenArr,
            nowNum = that.data.nowNum

        if (!chosenArr[nowNum - 1]) {
            chosenArr[nowNum - 1] = []
            chosenArr[nowNum - 1][index] = index
        } else {
            let temp_item = chosenArr[nowNum - 1].indexOf(index)

            if (temp_item === -1) {
                chosenArr[nowNum - 1][index] = index
            } else {
                chosenArr[nowNum - 1].splice(temp_item, 1, undefined)
            }
        }

        that.setData({
            chosenArr: chosenArr
        })

        let temp_ltArr = that.data.ltArr,
            ttid = that.data.ttid,
            temp_ttid = []

        for (let item of temp_ltArr) {
            temp_ttid.push(item.ttid)
        }

        for (let i = 0; i < chosenArr[nowNum - 1].length; i++) {
            if (chosenArr[nowNum - 1][i] === '' || typeof (chosenArr[nowNum - 1][i]) === 'undefined') {
                chosenArr[nowNum - 1].splice(i, 1)
                i = i - 1
            }
        }

        if (temp_ttid.indexOf(ttid) === -1) {
            temp_ltArr.push(
                {
                    ttid: ttid,
                    my_answer: chosenArr[nowNum - 1].join(',')
                }
            )
        } else {
            temp_ltArr[temp_ttid.indexOf(ttid)] = {
                ttid: ttid,
                my_answer: chosenArr[nowNum - 1].join(',')
            }
        }

        that.setData({
            ltArr: temp_ltArr
        })
    },

    /**
     * 交卷
     */
    handOver() {
        let that = this,
            ltArr = that.data.ltArr,
            num = that.data.num

        if (Number(num) > ltArr.length) {
            that.setData({
                noComplete: true
            })
        } else {
            that.setData({
                noComplete: false
            })
        }

        that.setData({
            handOver: true
        })
    },

    /**
     * 取消交卷
     */
    cancelHandOver() {
        this.setData({
            handOver: false
        })
    },

    /**
     * 确认交卷
     */
    confirmHandOver() {
        let that = this,
            url = 'Test_putTest',
            token = wx.getStorageSync('token'),
            ltid = that.data.ltid,
            ltArr = that.data.ltArr

        http._post(
            url,
            { token: token, ltid: ltid, ltArr: ltArr },
            res => {
                // console.log(res.data)
                let data = res.data
                if (data.re === '1') {
                    wx.showToast({
                        title: '交卷成功',
                        icon: 'none',
                        duration: 1000
                    })
                    wx.navigateTo({
                      url: '/pages/account/dar/dar',
                    })
                    that.setData({
                        handOver: false
                    })
                } else {
                    wx.showToast({
                        title: data.info,
                        icon: 'none',
                        duration: 3000
                    })
                    that.setData({
                        handOver: false
                    })
                }
            },
            err => {
                console.log(err)
            }
        )
        this.setData({
            noComplete: false
        })
    },

    /**
     * 获取题目信息
     */
    getQuestInfo(e) {
        let that = this,
            token = wx.getStorageSync('token'),
            ltid = that.data.ltid,
            ttid = e ? http.dataIndex(e)[1] : 0,
            url = 'Test_getQuestInfo',
            nowNum = that.data.nowNum,
            num = that.data.num

        if (e) {
            if (Number(nowNum) >= Number(num)) {
                wx.showToast({
                    title: '已经是最后一题了',
                    icon: 'none',
                    duration: 3000
                })

                return
            }
        } else {
            if (Number(nowNum) >= Number(num)) {
                that.setData({
                    has_miss: false
                })

                return
            }
        }

        nowNum++
        that.setData({
            nowNum: nowNum
        })

        http._post(
            url,
            { token: token, ltid: ltid, ttid: ttid },
            res => {
                // console.log(res.data)
                let data = res.data
                if (data.re === '1') {
                    that.setData({
                        queInfo: data.data,
                        t_type: data.data.tt_type,
                        ttid: data.data.ttid
                    })
                } else {
                    if (Number(nowNum) < Number(num)) {
                        that.setData({
                            has_miss: false,
                            nullData: {
                                img: http.imgUrl + 'null_data_1.jpg',
                                txt: data.info
                            }
                        })
                    }
                }
            },
            err => {
                console.log(err)
            }
        )
    },

    /**
     * 获取上一题
     */
    getQuestInfoPrev(e) {
        let that = this,
            token = wx.getStorageSync('token'),
            ltid = that.data.ltid,
            ttid = e ? http.dataIndex(e)[1] : 0,
            url = 'Test_getQuestInfo',
            nowNum = that.data.nowNum,
            num = that.data.num

        if (Number(nowNum) <= 1) {
            wx.showToast({
                title: '没有上一题了',
                icon: 'none',
                duration: 3000
            })

            return
        }

        nowNum--
        that.setData({
            nowNum: nowNum
        })

        http._post(
            url,
            { token: token, ltid: ltid, ttid: ttid, top: 1 },
            res => {
                // console.log(res.data)
                let data = res.data
                if (data.re === '1') {
                    that.setData({
                        queInfo: data.data,
                        t_type: data.data.tt_type,
                        ttid: data.data.ttid
                    })
                } else {
                    if (Number(nowNum) < Number(num)) {
                        that.setData({
                            has_miss: false,
                            nullData: {
                                img: http.imgUrl + 'null_data_1.jpg',
                                txt: data.info
                            }
                        })
                    }
                }
            },
            err => {
                console.log(err)
            }
        )
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let that = this,
            ltid = options.ltid || '0',
            count = options.count || '0',
            time = options.time || '0'
        // console.log(ltid)
        that.setData({
            ltid: ltid,
            num: count,
            nowNum: 0,
            time: util.formatSeconds(time),
        })

        that.getQuestInfo()  // 获取题目信息

        let countTime = setInterval(function () {
            time--
            that.setData({
                time: util.formatSeconds(time),
            })
            if (Number(time) === 0) {
                clearInterval(count)
                wx.showToast({
                    title: '测试时间结束，将为您自动交卷',
                    icon: 'none',
                    duration: 3000,
                    success: function () {
                        setTimeout(function () {
                            that.confirmHandOver()
                        }, 2000)
                    }
                })
            }
        }, 1000)
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})